package com.huike.report.MpService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huike.common.core.page.PageDomain;
import com.huike.common.core.page.TableSupport;
import com.huike.common.entity.MPTbContract;
import com.huike.report.MpMapper.ReportContractMapper;
import com.huike.report.MpService.ReportContractStaticService;
import com.huike.report.domain.dto.ReportContractStatisticsListDTO;
import com.huike.report.domain.dto.ReportSalesDeptStatisticsListVo;
import com.huike.report.domain.vo.LineChartVo;
import com.huike.report.domain.vo.LineSeriesVo;
import com.huike.report.domain.vo.ReportSubjectVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Max;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class ReportContractStaticServiceImpl implements ReportContractStaticService {

    @Autowired
    private ReportContractMapper reportContractMapper;

    /**
     * 客户统计明细列表
     * @param reportContractStatisticsListDTO
     * @return
     */
    @Override
    public Page<MPTbContract> pageList(ReportContractStatisticsListDTO reportContractStatisticsListDTO) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        PageHelper.startPage(pageDomain.getPageNum(),pageDomain.getPageSize());


        QueryWrapper<MPTbContract> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotEmpty(reportContractStatisticsListDTO.getChannel()),"channel",reportContractStatisticsListDTO.getChannel());
        wrapper.eq(ObjectUtils.isNotEmpty(reportContractStatisticsListDTO.getDeptId()),"dept_id",reportContractStatisticsListDTO.getDeptId());
        wrapper.eq(ObjectUtils.isNotEmpty(reportContractStatisticsListDTO.getCreateBy()),"create_by",reportContractStatisticsListDTO.getCreateBy());
        List<MPTbContract> tbContracts = reportContractMapper.selectList(wrapper);
        Page<MPTbContract> pageList = (Page<MPTbContract>) tbContracts;



        /*List<ReportContractStaticVo> reportContractStaticVoList = new ArrayList<>();
        for (MPTbContract mpTbContract : tbContracts) {
            ReportContractStaticVo reportContractStaticVo1 = new ReportContractStaticVo();
            BeanUtils.copyProperties(mpTbContract,reportContractStaticVo1);
            reportContractStaticVoList.add(reportContractStaticVo1);
        }*/
        //Page<ReportContractStaticVo> reportContractStaticVoPage = (Page<ReportContractStaticVo>) reportContractStaticVoList;
        //return reportContractStaticVoPage;
        return pageList;
        //return reportContractStaticVoList;

    }

    /**
     * 客户统计新增客户数报表
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public LineChartVo lineChart(String beginCreateTime, String endCreateTime) {
        LocalDate begin = LocalDate.parse(beginCreateTime, DateTimeFormatter.ISO_DATE);
        LocalDate end = LocalDate.parse(endCreateTime,DateTimeFormatter.ISO_DATE);
        //求最小时间的前一天客户数
        begin = begin.minusDays(1);
        LocalDateTime minBegin = LocalDateTime.of(begin, LocalTime.MAX);
        QueryWrapper<MPTbContract> wrapper = new QueryWrapper<>();
        wrapper.le("create_time",minBegin);
        Integer memberNumber = reportContractMapper.selectCount(wrapper);

        //当天新增人数集合
        List<Object> number = new ArrayList<>();
        //当天总人数集合
        List<Object> daysNumber = new ArrayList<>();
        //设置日期集合
        List<LocalDate> aAxis = new ArrayList<>();
        while(!Objects.equals(begin,end)){
            begin = begin.plusDays(1);
            aAxis.add(begin);
            //求时间的最大最小值
            LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(begin, LocalTime.MAX);
            QueryWrapper<MPTbContract> wrapperNew = new QueryWrapper<>();
            wrapperNew.le("create_time",endTime);
            wrapperNew.ge("create_time",beginTime);
            Integer integer = reportContractMapper.selectCount(wrapperNew);
            number.add(integer);
            memberNumber = memberNumber + integer;
            daysNumber.add(memberNumber);
        }
        //封装数据
        //新增客户数
        LineSeriesVo lineSeriesVo = new LineSeriesVo();
        lineSeriesVo.setName("新增客户数");
        lineSeriesVo.setData(number);
        //客户总数
        LineSeriesVo lineSeriesVo1 = new LineSeriesVo();
        lineSeriesVo1.setName("客户总数");
        lineSeriesVo1.setData(daysNumber);

        LineChartVo lineChartVo = new LineChartVo();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (LocalDate aAxi : aAxis) {
            lineChartVo.getxAxis().add(aAxi.format(dateTimeFormatter));
        }
        lineChartVo.getSeries().add(lineSeriesVo);
        lineChartVo.getSeries().add(lineSeriesVo1);
        return lineChartVo;
    }

    /**
     * 客户统计学科分布统计报表
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public List<ReportSubjectVo> reportSubject(String beginCreateTime, String endCreateTime) {
        LocalDate begin = LocalDate.parse(beginCreateTime, DateTimeFormatter.ISO_DATE);
        LocalDate end = LocalDate.parse(endCreateTime,DateTimeFormatter.ISO_DATE);

        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        List<ReportSubjectVo> reportSubjectVoList =
                reportContractMapper.selectSubjectNum(beginTime,endTime);

        return reportSubjectVoList;
    }

    /**
     * 销售统计归属部门明细列表
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public Page<ReportSalesDeptStatisticsListVo> deptStatisticsList(String beginCreateTime, String endCreateTime) {

        LocalDate begin = LocalDate.parse(beginCreateTime, DateTimeFormatter.ISO_DATE);
        LocalDate end = LocalDate.parse(endCreateTime,DateTimeFormatter.ISO_DATE);

        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        PageDomain pageDomain = TableSupport.buildPageRequest();
        PageHelper.startPage(pageDomain.getPageNum(),pageDomain.getPageSize());
        List<ReportSalesDeptStatisticsListVo> page = reportContractMapper.selectSoleNum(beginTime,endTime);
        Page<ReportSalesDeptStatisticsListVo> pageResult = (Page<ReportSalesDeptStatisticsListVo>)page;
        return pageResult;
    }
}
